﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoBackupDatabase.DBConection;
using AutoBackupDatabase.ENT;
using System.Data;
using System.Data.SqlClient;

namespace AutoBackupDatabase.DAL
{
    public class MATERIALDAL
    {
        private DbConnection conn;
        public MATERIALDAL()
        {
            conn = new DbConnection();
        }

        public List<MATERIALENT> GET_MATERIAL_ALL()
        {
            string query = string.Format("exec GET_MATERIAL_ALL ");
            DataTable ds = conn.executeGetQuery(query);
            List<MATERIALENT> List = new List<MATERIALENT>();
            foreach (DataRow item in ds.Rows)
            {
                MATERIALENT MATERIALent = new MATERIALENT();
                MATERIALent.MATERIALID = int.Parse(item["MATERIALID"].ToString());
                MATERIALent.MATERIALNAME = item["MATERIALNAME"].ToString();
                MATERIALent.QUANTITY = float.Parse(item["QUANTITY"].ToString());
                MATERIALent.UNITID = int.Parse(item["UNITID"].ToString());
                MATERIALent.UNITNAME = item["UNITNAME"].ToString();
                List.Add(MATERIALent);
            }
            return List;
        }
        public bool INSERTMATERIAL(MATERIALENT ent)
        {
            string query = "exec  INSERTMATERIAL @MATERIALNAME,@QUANTITY,@UNITID";
            SqlParameter[] sqlParameters = new SqlParameter[3];
            
            sqlParameters[0] = new SqlParameter("@MATERIALNAME", SqlDbType.NVarChar);
            sqlParameters[0].Value = Convert.ToString(ent.MATERIALNAME);
            sqlParameters[1] = new SqlParameter("@QUANTITY", SqlDbType.Float);
            sqlParameters[1].Value = ent.QUANTITY;
            sqlParameters[2] = new SqlParameter("@UNITID", SqlDbType.Int);
            sqlParameters[2].Value = ent.UNITID;
            return conn.executeInsertQuery(query, sqlParameters);
        }
        public bool UPDATEMATERIALBYMATERIALID(MATERIALENT ent)
        {
            string query = "exec  UPDATEMATERIALBYMATERIALID @MATERIALID,@MATERIALNAME,@QUANTITY,@UNITID";
            SqlParameter[] sqlParameters = new SqlParameter[4];
            sqlParameters[0] = new SqlParameter("@MATERIALID", SqlDbType.Int);
            sqlParameters[0].Value = ent.MATERIALID;
            sqlParameters[1] = new SqlParameter("@MATERIALNAME", SqlDbType.NVarChar);
            sqlParameters[1].Value = ent.MATERIALNAME;
            sqlParameters[2] = new SqlParameter("@QUANTITY", SqlDbType.Float);
            sqlParameters[2].Value = ent.QUANTITY;
            sqlParameters[3] = new SqlParameter("@UNITID", SqlDbType.Int);
            sqlParameters[3].Value = ent.UNITID;
            return conn.executeUpdateQuery(query, sqlParameters);
        }
        public bool DELTELEMATERIALBYMATERIALID(MATERIALENT ent)
        {
            string query = "exec  DELTELEMATERIALBYMATERIALID @MATERIALID";
            SqlParameter[] sqlParameters = new SqlParameter[1];
            sqlParameters[0] = new SqlParameter("@MATERIALID", SqlDbType.Int);
            sqlParameters[0].Value = Convert.ToString(ent.MATERIALID);
            return conn.executeDeleteQuery(query, sqlParameters);
        }
    }
}
